package com.lj.job.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lj.common.utils.excel.ExcelSheet;
import com.lj.common.utils.excel.ExcelUtil;
import com.lj.job.entity.JobLog;
import com.lj.job.mapper.JobLogMapper;
import com.lj.job.param.JobLogPageParam;
import com.lj.job.result.JobLogExportResult;
import com.lj.job.result.JobLogPageResult;
import com.lj.job.service.JobLogService;
import com.lj.mp.standard.StandardServiceImpl;
import com.lj.mp.utils.PageQueryUtils;
import org.springframework.stereotype.Service;

import java.io.OutputStream;
import java.util.List;

/**
 * <p>
 * 定时任务日志 服务实现类
 * </p>
 *
 * @author luojing
 * @since 2025-03-20 15:26:16
 */
@Service
public class JobLogServiceImpl extends StandardServiceImpl<JobLogMapper, JobLog> implements JobLogService {

    @Override
    public IPage<JobLogPageResult> page(JobLogPageParam param) {
        return this.page(PageQueryUtils.getPage(param), getQueryWrapper(param)).convert(JobLogPageResult::of);
    }

    private LambdaQueryWrapper<JobLog> getQueryWrapper(JobLogPageParam param) {
        return lambdaQueryWrapper()
                .eq(ObjectUtil.isNotEmpty(param.getJobId()), JobLog::getJobId, param.getJobId())
                .eq(ObjectUtil.isNotEmpty(param.getBeanName()), JobLog::getBeanName, param.getBeanName())
                .eq(ObjectUtil.isNotEmpty(param.getSucceed()), JobLog::getSucceed, param.getSucceed())
                ;
    }

    @Override
    public void exportExcel(JobLogPageParam param, OutputStream out) {
        List<JobLog> dataList = this.list(getQueryWrapper(param));
        List<JobLogExportResult> exportList = dataList.stream().map(JobLogExportResult::of).toList();
        ExcelUtil.writeExcel(out, new ExcelSheet<>(exportList, JobLogExportResult.class));
    }
}
